﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFMGlobals;

namespace PFMConfigurations
{
    public class PFMConfigurationManager
    {
        public List<Target> Targets { get; set; }
        public List<Schedule> Schedules { get; set; }
        private TargetManager targetManager;
        private ScheduleManager scheduleManager;
        public PFMConfigurationManager()
        {
            targetManager = new TargetManager();
            targetManager.Load();
            Targets = targetManager.Items;
            scheduleManager = new ScheduleManager();
            scheduleManager.Load();
            Schedules = scheduleManager.Items;
        }
        public bool SaveTarget()
        {
            targetManager.Items = this.Targets;
            return targetManager.Save();

        }
        public bool SaveSchedule()
        {
            scheduleManager.Items = this.Schedules;
            return scheduleManager.Save();
        }
        public List<Schedule> FindScheduleds(DateTime aDT)
        {
            List<Schedule> result = new List<Schedule>();
            foreach (Schedule s in this.Schedules)
            {
                if (IsScheduledTarget(s, aDT))
                    result.Add(s);
            }
            return result;
        }
        private string GetEventTime(DateTime eventTime)
        {
            string sResult = Convert.ToString(eventTime.Hour) + ":" + Convert.ToString(eventTime.Minute);

            if (eventTime.Minute < 10)
                sResult = Convert.ToString(eventTime.Hour) + ":0" + Convert.ToString(eventTime.Minute);

            return sResult;
        }
        private bool IsScheduledTarget(Schedule aSchedule, DateTime eventTime)
        {
            string sTime = "";
            string EventDay = "";
            bool bResult = false;

            EventDay = aSchedule.Interval.ToUpper();
            sTime = GetEventTime(eventTime);
            if (EventDay == PFMDefinitions.EVERYDAY)
            {
                if (aSchedule.Time == sTime)
                    bResult = true;
            }
            else
            {
                if (aSchedule.Time == sTime)
                {
                    switch (eventTime.DayOfWeek)
                    {
                        case System.DayOfWeek.Monday:
                            if (EventDay == PFMDefinitions.MONDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Tuesday:
                            if (EventDay == PFMDefinitions.TUESDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Wednesday:
                            if (EventDay == PFMDefinitions.WEDNESDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Thursday:
                            if (EventDay == PFMDefinitions.THURSDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Friday:
                            if (EventDay == PFMDefinitions.FRIDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Saturday:
                            if (EventDay == PFMDefinitions.SATURDAY)
                                bResult = true;
                            break;
                        case System.DayOfWeek.Sunday:
                            if (EventDay == PFMDefinitions.SUNDAY)
                                bResult = true;
                            break;
                    }
                }
            }
            return bResult;
        }
        public Target GetTargetFromSchedule(Schedule aSchedule)
        {
            //Target result = null;
            //foreach (Target t in this.Targets)
            //{
            //    if (aSchedule.TargetID.Equals(t.Id))
            //        result = t;
            //}
            //return result;
       
 return this.Targets != null?
     this.Targets.First(x => x.Id == aSchedule.TargetID)
     :null;
        }
    }
}
